package com.special.threademo.concurrency.conditionDemo;

import org.apache.ibatis.javassist.bytecode.stackmap.BasicBlock;

import javax.xml.stream.events.StartDocument;
import java.util.concurrent.CountDownLatch;
import java.util.stream.IntStream;

/**
 * Created with IDEA2020.3
 * author:LiuYC
 * DateTime:2021/1/19  17:27
 * countDown（） 等待和触发得语义
 * 语义得理解：四个子线程都完成后，主线程才开始执行
 */
public class Test {
    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(3);
        IntStream.range(0, 3).forEach(i -> new Thread(() -> {
            try {
                Thread.sleep(300);
                System.out.println("hello");
            } catch (InterruptedException exception) {
                exception.printStackTrace();

            } finally {
                countDownLatch.countDown();

            }
        }).start());
        System.out.println("启动子线程完毕");

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("主线程会执行完毕");
    }
}
